###########################################################################################
#Table B.7
#This table presents the spillover effect of ABBA on government allotment
###########################################################################################
#read in data
allocation_2016_data <- read.csv(paste0(AdminDataDir, "/fps_allocation_2016_backups.csv"))
allocation_el_data <- read.csv(paste0(AdminDataDir, "/fps_allocations_upto21July.csv"))
study_codes_data <- readstata13::read.dta13(paste0(AdminDataDir, "/secc_blocks_all_tags_study_codes.dta"))


#assign blocks to study and non-study districts
study_dist <- c("BOKARO", "DUMKA", "GARHWA", "LOHARDAGA", "PAKUR", "PALAMU", "WEST SINGHBHUM",
                "RAMGARH", "SAHEBGANJ", "SARAIKELA KHARSAWAN")
non_study_dist <- c("DEOGHAR", "DHANBAD", "GODDA", "GUMLA", "KODERMA", "LATEHAR", "SIMDEGA")

#keep only records for mar, apr, may and for study and non study districts
allocation_epos_2016 <- subset(allocation_2016_data, districtName %in% study_dist | districtName %in% non_study_dist)
allocation_epos_2016 <- subset(allocation_epos_2016, month_id %in% c(3,4,5))

allocation_epos_2016$study_dist <- 0
allocation_epos_2016$study_dist[allocation_epos_2016$districtName %in% study_dist] <- 1

#merge with study codes data
study_codes_data <- study_codes_data[c("rgi_district_code", "treatment")]
allocation_epos_2016 <- merge(allocation_epos_2016, study_codes_data, by = "rgi_district_code")

#keep non-treatment data
bl_allocation_data <- subset(allocation_epos_2016, treatment != "Treatment")

#remove duplicate rows
bl_allocation_data <- bl_allocation_data[!duplicated(bl_allocation_data[,"id"]),]
bl_allocation_data <- bl_allocation_data[!duplicated(bl_allocation_data[,c("dealer_code", "month_id", "quantity")]),]

#add up all individual transactions
ind_allocation_data <- unique(bl_allocation_data$dealer_code)
ind_allocation_data <- as.data.frame(ind_allocation_data)
colnames(ind_allocation_data) <- "dealer_code"

return_study_dist <- function(x){
  ori_spot <- which(bl_allocation_data$dealer_code %in% x)[1]
  return(bl_allocation_data$study_dist[ori_spot])
}

return_dist_code <- function(x){
  ori_spot <- which(bl_allocation_data$dealer_code %in% x)[1]
  return(bl_allocation_data$rgi_district_code[ori_spot])
}

return_block_code <- function(x){
  ori_spot <- which(bl_allocation_data$dealer_code %in% x)[1]
  return(bl_allocation_data$rgi_block_code[ori_spot])
}

return_treatment_status <- function(x){
  ori_spot <- which(bl_allocation_data$dealer_code %in% x)[1]
  return(bl_allocation_data$treatment[ori_spot])
}

ind_allocation_data$study_dist <- sapply(ind_allocation_data$dealer_code, return_study_dist)
ind_allocation_data$treatment <- sapply(ind_allocation_data$dealer_code, return_treatment_status)
ind_allocation_data$rgi_block_code <- sapply(ind_allocation_data$dealer_code, return_block_code)
ind_allocation_data$rgi_district_code <- sapply(ind_allocation_data$dealer_code, return_dist_code)

ind_allocation_data$quantity_3 <- NA
ind_allocation_data$quantity_4 <- NA
ind_allocation_data$quantity_5 <- NA
for(t in c(3,4,5)){
  var_to_replace <- paste0("quantity_", t)
  for(i in 1:nrow(ind_allocation_data)){
    all_counts <- which(bl_allocation_data$dealer_code %in% ind_allocation_data$dealer_code[i])
    sum <- 0
    for(a in all_counts){
      if(bl_allocation_data$month_id[a] == t){
        sum <- bl_allocation_data$quantity[a] + sum
      }
    }
    ind_allocation_data[[var_to_replace]][i] <- sum
    print(paste0("Done with ", i, " simulation(s)"))
  }
}

ind_allocation_data$bl_quantity <- (ind_allocation_data$quantity_3 + ind_allocation_data$quantity_4 + ind_allocation_data$quantity_5)/3

#deal with el data
allocation_el_data
#keep only records for jan, feb, mar and for study and non study districts
allocation_el_data <- subset(allocation_el_data, districtName %in% study_dist | districtName %in% non_study_dist)
allocation_el_data <- subset(allocation_el_data, month_id %in% c(1,2,3))

allocation_el_data$study_dist <- 0
allocation_el_data$study_dist[allocation_el_data$districtName %in% study_dist] <- 1

#merge with study codes data
allocation_el_data <- merge(allocation_el_data, study_codes_data, by = "rgi_district_code")

#keep non-treatment data
allocation_el_data <- subset(allocation_el_data, treatment != "Treatment")

#remove duplicate rows
allocation_el_data <- allocation_el_data[!duplicated(allocation_el_data[,"id"]),]
allocation_el_data <- allocation_el_data[!duplicated(allocation_el_data[,c("dealer_code", "month_id", "quantity")]),]

#add up all individual transactions
ind_allocation_el_data <- unique(allocation_el_data$dealer_code)
ind_allocation_el_data <- as.data.frame(ind_allocation_el_data)
colnames(ind_allocation_el_data) <- "dealer_code"

return_study_dist <- function(x){
  ori_spot <- which(allocation_el_data$dealer_code %in% x)[1]
  return(allocation_el_data$study_dist[ori_spot])
}

return_dist_code <- function(x){
  ori_spot <- which(allocation_el_data$dealer_code %in% x)[1]
  return(allocation_el_data$rgi_district_code[ori_spot])
}

return_block_code <- function(x){
  ori_spot <- which(allocation_el_data$dealer_code %in% x)[1]
  return(allocation_el_data$rgi_block_code[ori_spot])
}

return_treatment_status <- function(x){
  ori_spot <- which(allocation_el_data$dealer_code %in% x)[1]
  return(allocation_el_data$treatment[ori_spot])
}

ind_allocation_el_data$study_dist <- sapply(ind_allocation_el_data$dealer_code, return_study_dist)
ind_allocation_el_data$treatment <- sapply(ind_allocation_el_data$dealer_code, return_treatment_status)
ind_allocation_el_data$rgi_block_code <- sapply(ind_allocation_el_data$dealer_code, return_block_code)
ind_allocation_el_data$rgi_district_code <- sapply(ind_allocation_el_data$dealer_code, return_dist_code)

ind_allocation_el_data$quantity_1 <- NA
ind_allocation_el_data$quantity_2 <- NA
ind_allocation_el_data$quantity_3 <- NA
for(t in c(1,2,3)){
  var_to_replace <- paste0("quantity_", t)
  for(i in 1:nrow(ind_allocation_el_data)){
    all_counts <- which(allocation_el_data$dealer_code %in% ind_allocation_el_data$dealer_code[i])
    sum <- 0
    for(a in all_counts){
      if(allocation_el_data$month_id[a] == t){
        sum <- allocation_el_data$quantity[a] + sum
      }
    }
    ind_allocation_el_data[[var_to_replace]][i] <- sum
    print(paste0("Done with ", i, " simulation(s)"))
  }
}

return_baseline <- function(x){
  ori_spot <- which(ind_allocation_data$dealer_code %in% x)
  return(ind_allocation_data$bl_quantity[ori_spot])
}

ind_allocation_el_data$bl_var <- sapply(ind_allocation_el_data$dealer_code, return_baseline)
ind_allocation_el_data$bl_var <- as.numeric(ind_allocation_el_data$bl_var)

ind_allocation_el_data <- reshape(ind_allocation_el_data, 
                                  varying = c("quantity_1", "quantity_2", "quantity_3"),
                                  v.names = c("quantity"),
                                  timevar = c("time"),
                                  times = c("jan", "feb", "mar"),
                                  direction = "long",
                                  idvar = "dealer_code")

block_data <- aggregate(ind_allocation_el_data[,c("quantity", "bl_var")], by = list(block_code = ind_allocation_el_data$rgi_block_code,
                                                                                    time = ind_allocation_el_data$time), FUN = sum, na.rm = T)

return_district <- function(x){
  ori_spot <- which(ind_allocation_el_data$rgi_block_code %in% x)[1]
  return(ind_allocation_el_data$study_dist[ori_spot])
}

return_district_code <- function(x){
  ori_spot <- which(ind_allocation_el_data$rgi_block_code %in% x)[1]
  return(ind_allocation_el_data$rgi_district_code[ori_spot])
}

block_data$study_dist <- sapply(block_data$block_code, return_district)
block_data$district_code <- sapply(block_data$block_code, return_district_code)

block_data$bl_var_mi <- 0
block_data$bl_var_mi[is.na(block_data$bl_var)] <- 1
block_data$bl_var[block_data$bl_var_mi == 1] <- mean(block_data$bl_var, na.rm = TRUE)


disbursement_reg <- miceadds::lm.cluster(data = block_data, formula = quantity ~ study_dist + bl_var, cluster = block_data$district_code)
disbursement_fake <- lm(data = block_data, formula = quantity ~ study_dist + bl_var)

ses <- list(c(0, summary(disbursement_reg)[,2]))
c_mean <- scales::comma_format()(round(mean(block_data$quantity[block_data$study_dist == 0], na.rm = TRUE), 0))

spillover <- 
  stargazer::stargazer(disbursement_fake, 
                       header = FALSE, 
                       align = TRUE, 
                       float = FALSE, 
                       dep.var.labels = c("Quantity Alloted"),
                       covariate.labels = c("Treated District"), 
                       star.cutoffs = NA,
                       omit = c("Constant", "bl_var"),
                       omit.stat = c("f", "ser", "rsq"),
                       omit.table.layout = "n",
                       add.lines = list(c("Control mean", paste0("\\multicolumn{1}{c}{", c_mean, "}"))),
                       table.layout = "-d-t-sa-",
                       notes = " ",
                       digits = 2, 
                       se = ses)
obs <- spillover[10]
adj_rsq <- spillover[11]
cntrl_mm <- spillover[12]

spillover[10] <- adj_rsq
spillover[11] <- cntrl_mm
spillover[12] <- obs

cat(paste(spillover, collapse = "\n"), "\n", file = paste0(OutputDir, "TableB_7.tex"))
